<?php

namespace app\config;
use AlibabaCloud\SDK\Cloudauth\V20190307\Cloudauth;
use AlibabaCloud\SDK\Cloudauth\V20190307\Models\InitFaceVerifyRequest;
use AlibabaCloud\SDK\Cloudauth\V20190307\Models\InitFaceVerifyResponse;
use AlibabaCloud\Tea\Utils\Utils;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;

class InitFaceVerify
{

    /**
     * @param string $endpoint
     * @return Cloudauth
     */
    public static function createClient($endpoint)
    {
        $阿里云 = config("Aliyun");
        $config = new Config([
            // 您的AccessKey ID。
            "accessKeyId" => $阿里云['accessKeyId'],
            // 您的AccessKey Secret。
            "accessKeySecret" => $阿里云['accessKeySecret'],
            // 设置HTTP代理。
            // "httpProxy" => "<http://xx.xx.xx.xx:xxxx>",
            // 设置HTTPS代理
            // "httpsProxy" => "<https://xx.xx.xx.xx:xxxx>",
            "endpoint" => $endpoint

        ]);
        return new Cloudauth($config);
    }

    /**
     * @return void
     */
    public static function main($member_id,$outerOrderNo,$certName,$certNo,$metaInfo,$type=1)
    {
        $阿里云 = config("Aliyun");
        switch ($type){
            case 1://实名认证回调
                $callbackUrl = $阿里云['callbackUrl'];
                break;
            case 2://找回密码回调
                $callbackUrl = $阿里云['callbackPassUrl'];
                break;
            default:
                $callbackUrl = $阿里云['callbackUrl'];
        }
        $data = [
            // 请输入场景ID。
            "sceneId" => $阿里云['sceneId'],
            "outerOrderNo" => $outerOrderNo,
            // 要接入的认证方案。
            "productCode" => "ID_PRO",
            // 要进行活体检测的类型。
            "model" => "LIVENESS",
            "certType" => "IDENTITY_CARD",
            "certName" => $certName,
            "certNo" => $certNo,
            // MetaInfo环境参数，需要通过客户端SDK获取。
            "metaInfo" =>$metaInfo,
//            "metaInfo" => '{"zimVer":"3.0.0","appVersion": "1","bioMetaInfo": "4.1.0:11501568,0","appName": "com.aliyun.antcloudauth","deviceType": "ios","osVersion": "iOS 10.3.2","apdidToken": "","deviceModel": "iPhone9,1"}',
            // "mobile" => "<xxx>",
            // "ip" => "<114.xxx.xxx.xxx>",
            // "userId" => "<12345xxxx>",
             "callbackUrl" => $callbackUrl,
             "callbackToken" => $member_id.'_'.$阿里云['callbackToken'],
            // 如需开启个人信息加密传输。
            // "encryptType" => "SM2",
            // "certName" => "BCRD/7ZkNy7Q*****M1BMBezZe8GaYHrLwyJv558w==",
            // "certNo" => "BMjsstxK3S4b1YH*****Pet8ECObfxmLN92SLsNg=="
        ];
        $request = new InitFaceVerifyRequest($data);
        // 推荐，支持服务路由。
        $response = self::initFaceVerifyAutoRoute($request);
        // 不支持服务自动路由。
//        $response = self::initFaceVerify("cloudauth.cn-shanghai.aliyuncs.com", $request);
        return $response;
    }

    /**
     * @param InitFaceVerifyRequest $request
     * @return InitFaceVerifyResponse
     */
    public static function initFaceVerifyAutoRoute(InitFaceVerifyRequest $request)
    {
        $endpoints = [
            "cloudauth.cn-shanghai.aliyuncs.com",
            "cloudauth.cn-beijing.aliyuncs.com"
        ];
        foreach ($endpoints as $endpoint) {
            try {
                $response = self::initFaceVerify($endpoint, $request);
                if (Utils::equalString("500", $response->body->code)) {
                    continue;
                }
                return $response;
            } catch (Exception $err) {
                var_dump($err);die;
                if (!($err instanceof TeaError)) {
                    $err = new TeaError([], $err->getMessage(), $err->getCode(), $err);
                }
                // 网络异常，切换到下个区域调用。
                if (Utils::equalString("ServiceUnavailable", $err->code)) {
                    continue;
                }
            }
        }
        return null;
    }

    /**
     * @param string $endpoint
     * @param InitFaceVerifyRequest $request
     * @return InitFaceVerifyResponse
     */
    public static function initFaceVerify($endpoint, $request)
    {
        $client = self::createClient($endpoint);
        // 创建RuntimeObject实例并设置运行参数。
        $runtime = new RuntimeOptions([]);
        $runtime->readTimeout = 10000;
        $runtime->connectTimeout = 10000;
        return $client->initFaceVerify($request, $runtime);
    }
}
